'use strict';

const Controller = require("./base");
const path = require("path");
const ExcelJS = require('exceljs');

const getArrByStream = async (stream)=>{
  const workbook = new ExcelJS.Workbook();
  await workbook.xlsx.read(stream);
  let excelData = []; 
  workbook.worksheets[0].eachRow((row, rowNumber) => {
    let rowData = [];
    row.eachCell((cell, colNumber) => {
      rowData.push(cell.value);
    });
    excelData.push(rowData);
  }); 
  return excelData
}
class HomeController extends Controller {  
  async prescription() { /* 最核心的一个录入 看怎样处理*/
    const excelData  = await getArrByStream(await this.ctx.getFileStream());
    await this.service.excel.saveTable(excelData.slice(1).map(e=>{ return { name:e[0],c1:e[1],c2:e[2],c3:e[3], } }),'table3')
    /* 这里还要录入 药材和症状 关键信息 */
    this.ctx.body = excelData;
  }
  async medic() {
    const excelData  = await getArrByStream(await this.ctx.getFileStream());
    await this.service.excel.saveTable(excelData.slice(1).map(e=>{ return { name:e[0],c1:e[1],c2:e[2],c3:e[3], } }),'table1')
    this.ctx.body = excelData;
  }
  async symptom() {
    const excelData  = await getArrByStream(await this.ctx.getFileStream());
    await this.service.excel.saveTable(excelData.slice(1).map(e=>{ return { name:e[0],c1:e[1],c2:e[2],c3:e[3], } }),'table2')
    this.ctx.body = excelData;
  } 
}

module.exports = HomeController;
